// MainFrm.cpp : implmentation of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"

#include "aboutdlg.h"
#include "dibexView.h"
#include "MainFrm.h"

BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
  if(CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg))
    return TRUE;

  return m_view.PreTranslateMessage(pMsg);
}

BOOL CMainFrame::OnIdle()
{
  UIUpdateToolBar();
  return FALSE;
}

LRESULT CMainFrame::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
  // create command bar window
  HWND hWndCmdBar = m_CmdBar.Create(m_hWnd, rcDefault, NULL, ATL_SIMPLE_CMDBAR_PANE_STYLE);
  // attach menu
  m_CmdBar.AttachMenu(GetMenu());
  // load command bar images
  m_CmdBar.LoadImages(IDR_MAINFRAME);
  // remove old menu
  SetMenu(NULL);

  HWND hWndToolBar = CreateSimpleToolBarCtrl(m_hWnd, IDR_MAINFRAME, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE);

  CreateSimpleReBar(ATL_SIMPLE_REBAR_NOBORDER_STYLE);
  AddSimpleReBarBand(hWndCmdBar);
  AddSimpleReBarBand(hWndToolBar, NULL, TRUE);

  CreateSimpleStatusBar();

  m_hWndClient = m_view.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, WS_EX_CLIENTEDGE);

  UIAddToolBar(hWndToolBar);
  UISetCheck(ID_VIEW_TOOLBAR, 1);
  UISetCheck(ID_VIEW_STATUS_BAR, 1);

  // register object for message filtering and idle updates
  CMessageLoop* pLoop = _Module.GetMessageLoop();
  ATLASSERT(pLoop != NULL);
  pLoop->AddMessageFilter(this);
  pLoop->AddIdleHandler(this);

  return 0;
}

LRESULT CMainFrame::OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  PostMessage(WM_CLOSE);
  return 0;
}

LRESULT CMainFrame::OnFileNew(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  // TODO: add code to initialize document

  return 0;
}

LRESULT CMainFrame::OnFileOpen(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  CFileDialog dlg(TRUE, "png", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
    "Image files (*.png,*.jpg,*.gif)\0*.png;*.jpg;*.gif\0"
    "PNG files (*.png)\0*.png\0"
    "JPEG files (*.jpg,*.jpeg)\0*.JPG;*.JPEG\0"
    "GIF files (*.gif)\0*.gif\0"
    "All Files (*.*)\0*.*\0", m_hWnd);

  if(dlg.DoModal() == IDOK)
  {
    img.load(dlg.m_szFileName);
    if(!img.is_valid())
      AtlMessageBox(m_hWnd,"Cannot load that image");
    else
      ::InvalidateRect(m_hWndClient,0,0);
  }

  return 0;
}


LRESULT CMainFrame::OnViewToolBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  static BOOL bVisible = TRUE;  // initially visible
  bVisible = !bVisible;
  CReBarCtrl rebar = m_hWndToolBar;
  int nBandIndex = rebar.IdToIndex(ATL_IDW_BAND_FIRST + 1); // toolbar is 2nd added band
  rebar.ShowBand(nBandIndex, bVisible);
  UISetCheck(ID_VIEW_TOOLBAR, bVisible);
  UpdateLayout();
  return 0;
}

LRESULT CMainFrame::OnViewStatusBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  BOOL bVisible = !::IsWindowVisible(m_hWndStatusBar);
  ::ShowWindow(m_hWndStatusBar, bVisible ? SW_SHOWNOACTIVATE : SW_HIDE);
  UISetCheck(ID_VIEW_STATUS_BAR, bVisible);
  UpdateLayout();
  return 0;
}

LRESULT CMainFrame::OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  CAboutDlg dlg;
  dlg.DoModal();
  return 0;
}
